﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DotNetNuke.Common;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Modules.Actions;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Services.Localization;
using System.Xml;


namespace SE.HTMLRepeater
{
    public partial class TemplateEditor : PortalModuleBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!IsPostBack)
                {
                    object setting = Settings["template"];
                    if (setting != null)
                    {
                        string settingValue = setting.ToString();
                        txtTemplate.Text  = settingValue;
                    }

                    setting = Settings["pageSize"];
                    if (setting != null)
                    {
                        string settingValue = setting.ToString();
                        txPageSize.Text = settingValue;
                    }
                    setting = Settings["flowLayout"];
                    if (setting != null)
                    {
                        string settingValue = setting.ToString();
                        chkFlowLayout.Checked = Convert.ToBoolean(settingValue);
                    }

                }
            }
            catch (Exception ex)
            {
                Exceptions.ProcessModuleLoadException(this, ex);
            }
        }
        protected void cmdUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                ModuleController controller = new ModuleController();
                controller.UpdateTabModuleSetting(this.TabModuleId, "template", txtTemplate.Text);
                controller.UpdateTabModuleSetting(this.TabModuleId, "pageSize", txPageSize.Text);
                controller.UpdateTabModuleSetting(this.TabModuleId, "flowLayout", chkFlowLayout.Checked.ToString());

                if (((LinkButton) sender).CommandName == "Update")
                {Response.Redirect(Globals.NavigateURL(), true);}

            }
            catch (Exception ex)
            {
                Exceptions.ProcessModuleLoadException(this, ex);
            }
        }

        protected void cmdCancel_Click(object sender, EventArgs e)
        {
          Response.Redirect(Globals.NavigateURL(), true);
        }
        protected void cmdSaveTemplate_Click(object sender, EventArgs e)
        {
            try
            {
                //Get Content and save as normal. 
                cmdUpdate_Click(sender, e);
                //Get Content and save to FCK template xml
                Response.Redirect(Globals.NavigateURL("ManageExistingTemplates","mid",this.ModuleId.ToString(),"save","true"), true);

                //string Html = txtTemplate.Text;
                //SaveContentAsPermTemplate(Html);
                
            }
            catch (Exception ex)
            {
                Exceptions.ProcessModuleLoadException(this, ex);
            }
            
        }




    }
}